// pages/uploadTask/uploadTask.js
import {taskSave,taskList} from '../../Interface/taskInterface'
import {debounce} from '../../utils/repeat'
let focusIndex = null
let name = ""
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		colorList: [
			"#fc403a",
			"#f9b137",
			"#ff6500",
			"#35f851",
			"#2cf957",
			"#35dcfa",
			"#3f43fa",
			"#c03cfc",
			"#969696",
			"#000000"
		],
		lableList:[
			{
				color: '#fc403a',
				title: ''
			},
		],
		squadid: "",
		squadName: "添加小分队"
	},
	bindtapName(event){
		name = event.detail.value
	},
	getFocus(event){
		focusIndex = event.currentTarget.dataset.index
	},
	// 数值转换
	getInputValue(event){
		let index = event.currentTarget.dataset.index
		let value = event.detail.value
		this.data.lableList[index].title = value
		this.setData({
			lableList: this.data.lableList
		})
	},
	// 切换当前选中input的颜色
	tabColor(event){
		let color = event.currentTarget.dataset.color
		this.data.lableList[focusIndex].color = color
		this.setData({
			lableList: this.data.lableList
		})
	},
	tabplus(){
		this.setData({
			lableList: this.data.lableList.concat({
				color: '#fc403a',
				title: ''
			},)
		})
	},
	tabreduces(event){
		let index = event.currentTarget.dataset.index;
		this.data.lableList.splice(index,1)
		this.setData({
			lableList: this.data.lableList
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

	},
	bindPlus(){
		wx.navigateTo({
			url: '../obtainstudent/obtainstudent?type=1'
		})
	},
	// 上传
	upload: debounce(async function(event){
		if(name===''){
			wx.showToast({
				title: '请输入名称',
				duration: 1000
			})
			return
		}
		if(this.data.squadid===''){
			wx.showToast({
				title: '请选择小分队',
				duration: 1000
			})
			return
		}
		let lableList = this.data.lableList.filter(item=>{
			return item.title === ''
		})
		if(lableList.length>0){
			wx.showToast({
				title: '请填写标签',
				duration: 1000
			})
			return
		}
		taskSave({
			data:{
				name,
				squadid: this.data.squadid,
				labellist: this.data.lableList
			}
		}).then(res=>{
			wx.showModal({
				title: "提示",
				content: "创建任务成功",
				success(res){
					wx.navigateBack({
						delta: 1
					})
				}
			})
		})
	}),
	// 获取当前用户
	onUser(user,item){
		this.setData({
			squadid: item._id,
			squadName: item.name
		})
		return true
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})